package com.shixi.shixitraining.shixitraining.ui.tabapply.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.TabhostBaseFragment;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabapply.adapter.ApplyTrainAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabapply.entity.ApplyTrainBean;
import com.shixi.shixitraining.shixitraining.ui.tabapply.activity.TrainApplyDetailActivity;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.util.UserManager;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleItemClickListener;

import java.util.List;

import retrofit2.Call;

import static com.shixi.shixitraining.shixitraining.constant.Constants.APPLYJOB;

/**
 * Created by shenyan on 2018/7/17.
 */

public class ApplyJobFragment extends TabhostBaseFragment {

    private TextView mDataTv;

    @Override
    protected int getlayoutId() {
        return R.layout.fragment_job_training;
    }

    @Override
    protected void initPager() {
        mDataTv = (TextView) mContentView.findViewById(R.id.tv_nodata);

        String token = UserManager.getInstance().getToken();
        String userId = UserManager.getInstance().getUserId();

        initTrain(token, userId);
    }

    private void initTrain(String token, String userId) {
//        String url = "http://api.shixigroup.com//myapply/train?userId=3&token=033e191242ed1f4b503f9bd6fca6e2d4";
          String url=APPLYJOB+"&userId="+userId+"&token="+token;

        HttpUtil.getInstance().getApiInterface().applyJob(url).enqueue(new CallbackImple<ApplyTrainBean>() {
            @Override
            public void onError(Call<ApplyTrainBean> call, Throwable t) {
                ToastUtil.showShortToast(getContext(), t.getMessage());
            }

            @Override
            public void onSuccess(Call<ApplyTrainBean> call, ApplyTrainBean response) {

                if (response.getData() != null && response.getData().size() > 0) {
                    if (response.getStatus() == 200) {
                        final List<ApplyTrainBean.DataBean> data = response.getData();
                        RecyclerView rcview_job = (RecyclerView) mContentView.findViewById(R.id.rcview_job_training);
                        LinearLayoutManager manager = new LinearLayoutManager(getContext());
                        rcview_job.setLayoutManager(manager);
                        ApplyTrainAdapter adapter = new ApplyTrainAdapter(getContext(), data);
                        rcview_job.setAdapter(adapter);
                        rcview_job.addOnItemTouchListener(new RecycleItemClickListener(getContext()) {
                            @Override
                            public void itemClickListener(View view, int position) {
                                ApplyTrainBean.DataBean dataBean = data.get(position);
                                String number = dataBean.getNumber();
                                Bundle bundle = new Bundle();
                                bundle.putString("number", number);

                                Intent intent = new Intent(getContext(), TrainApplyDetailActivity.class);
                                intent.putExtras(bundle);

                                getContext().startActivity(intent);
                            }
                        });

                    } else {
                        ToastUtil.showShortToast(getContext(), response.getMsg());
                    }

                } else {
                    mDataTv.setVisibility(View.VISIBLE);
                    ToastUtil.showShortToast(getContext(), "数据为空");
                }


            }
        });


    }


}
